//
//  BIDShowDocView.m
//  shangwuting
//
//  Created by mal on 13-12-26.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDShowDocView.h"
#import "BIDGlobalMethods.h"
#import "BIDCustomSpinnerView.h"

@implementation BIDShowDocView
@synthesize docViewTitle;
@synthesize docWebView;
@synthesize fileNameURL;
@synthesize closeBtn;
@synthesize spinnerView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initView];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)initView
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGSize titleSize = CGSizeMake(screenSize.width-30*2, 44.0f);
    CGSize btnSize = CGSizeMake(30, 30);
    CGRect btnFrame = CGRectMake(screenSize.width-btnSize.width, (titleSize.height-btnSize.height)/2, btnSize.width, btnSize.height);
    CGRect docViewTitleFrame = CGRectMake(30, 0, titleSize.width, titleSize.height);
    docViewTitle = [[UILabel alloc] initWithFrame:docViewTitleFrame];
    [docViewTitle setBackgroundColor:[UIColor clearColor]];
    [docViewTitle setTextAlignment:NSTextAlignmentCenter];
    [docViewTitle setTextColor:[UIColor whiteColor]];
    
    closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = btnFrame;
    [closeBtn setTitle:@"X" forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [closeBtn addTarget:self action:@selector(closeBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
    [self setBackgroundColor:[UIColor whiteColor]];
    CGSize docWebViewSize = CGSizeMake(screenSize.width, screenSize.height-docViewTitle.frame.size.height);
    CGRect docWebViewFrame = CGRectMake(0, docViewTitle.frame.size.height, docWebViewSize.width, docWebViewSize.height);
    docWebView = [[UIWebView alloc] initWithFrame:docWebViewFrame];
    docWebView.scalesPageToFit = YES;
    docWebView.delegate = self;
    
    //标题的背景
    UIView *viewForTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 44.0f)];
    [viewForTitle setBackgroundColor:[UIColor blackColor]];
    [self addSubview:viewForTitle];
    [self addSubview:docViewTitle];
    [self addSubview:closeBtn];
    [self addSubview:docWebView];
    
    //spinner view
    spinnerView = [[BIDCustomSpinnerView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    spinnerView.content = @"正在打开..";
}

- (void)openDoc:(NSString*)fileName
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    docViewTitle.text = fileName;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    NSArray *arr = [fileName componentsSeparatedByString:@"."];
    NSString *strExtension = arr[1];
    NSURL *libraryURL = [BIDGlobalMethods getLibraryPath];
    NSURL *fileURL = [libraryURL URLByAppendingPathComponent:[[NSString alloc] initWithFormat:@"downloads/doc/%@", fileName]];
    if([strExtension isEqualToString:@"txt"])
    {
        [self openTxt:fileURL];
    }
    else
    {
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:fileURL];
        [docWebView loadRequest:urlRequest];
    }
}
- (void)openDoc:(NSString *)fileName path:(NSURL *)desURL
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    docViewTitle.text = fileName;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    NSArray *arr = [fileName componentsSeparatedByString:@"."];
    NSString *strExtension = arr[1];
    NSURL *fileURL = [desURL URLByAppendingPathComponent:[[NSString alloc] initWithFormat:@"%@", fileName]];
    if([strExtension isEqualToString:@"txt"])
    {
        [self openTxt:fileURL];
    }
    else
    {
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:fileURL];
        [docWebView loadRequest:urlRequest];
    }
}

- (void)openTxt:(NSURL *)url
{
    NSStringEncoding *useEncoding = nil;
    NSString *body = [NSString stringWithContentsOfURL:url usedEncoding:useEncoding error:nil];
    if(!body)
    {
        NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        body = [NSString stringWithContentsOfURL:url encoding:enc error:nil];
    }
    if(!body)
    {
        body = [NSString stringWithContentsOfURL:url encoding:0x80000631 error:nil];
    }
    if(body)
    {
        body = [[NSString alloc] initWithFormat:@"<html><body><pre>%@</pre></body></html>", body];
        [docWebView loadHTMLString:body baseURL:nil];
    }
    else
    {
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        [docWebView loadRequest:urlRequest];
    }
}

- (void)closeBtnHandler
{
    if(docWebView.loading) [docWebView stopLoading];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self removeFromSuperview];
}

#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [spinnerView showTheView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [spinnerView dismissTheView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [spinnerView dismissTheView];
    NSLog(@"%@", error.description);
}

@end
